package com.gm.wholesale.bluetooth.weightBlue;

import android.app.Activity;
import android.content.Context;
import android.text.TextUtils;

import com.gm.wholesale.bluetooth.BluetoothUtil;
import com.gm.wholesale.bluetooth.DeviceManger;
import com.gm.wholesale.bluetooth.WeightBlueType;
import com.gm.wholesale.bluetooth.bean.DeviceBean;
import com.gm.wholesale.bluetooth.interfaces.OnConnectionBluListener;
import com.gm.wholesale.bluetooth.interfaces.OnGetDeviceListener;

public class WeightBlueManger {

    private static KHWeightBluetooth mKHWeightBluetooth; //坤宏蓝牙秤
    private static WXLWeightBluetooth mWXLWeightBluetooth;//无限量蓝牙秤
    private static WeightBlueManger weightBlueManger;
    private WeightBlueManger() {
    }
    public void setFailState(){
        if (mKHWeightBluetooth!=null){
            mKHWeightBluetooth.setFailState();
        }
        if (mWXLWeightBluetooth!=null){
            mWXLWeightBluetooth.setFailState();
        }
    }
    public static WeightBlueManger newInstance() {
        if (weightBlueManger == null) {
            weightBlueManger = new WeightBlueManger();
        }
        return weightBlueManger;
    }
    public boolean isConnection(){
        if (mKHWeightBluetooth!=null){
            if (BluetoothUtil.newInstance().isConnection(mKHWeightBluetooth.mBluetoothService)){
                return true;
            }
        }
        if (mWXLWeightBluetooth!=null){
            if (BluetoothUtil.newInstance().isConnection(mWXLWeightBluetooth.mBluetoothService)){
                return true;
            }
        }
        return false;
    }

    public  void initWeightBlue(Context context) {
        mKHWeightBluetooth = KHWeightBluetooth.getBluetoothManger(context);
        mWXLWeightBluetooth = WXLWeightBluetooth.getBluetoothManger(context);
    }


    //todo 链接kunhong 秤
    private void connectionKH(Context context, String device, OnConnectionBluListener listener) {
        if (mKHWeightBluetooth == null) return;
        mKHWeightBluetooth.setOnConnectionListener(listener)
                .startConnectWeightDevice(context, device);

    }

    // todo 连接无限量秤
    private void connectionWXL(Context context, String device, OnConnectionBluListener listener) {
        if (mWXLWeightBluetooth==null)return;
        mWXLWeightBluetooth.setOnConnectionListener(listener)
                .startConnectWeightDevice(context,device);
    }

    //主界面调用，连接蓝牙秤
    public void connectionWeightBlue(Context context, OnConnectionBluListener listener) {
        DeviceBean deviceBean = DeviceManger.getDeviceManger().getWeightDevice();
        //有保存的设备信息直接连接
        if (deviceBean != null && !TextUtils.isEmpty(deviceBean.getAddress())) {
            if (deviceBean.getName().startsWith(WeightBlueType.getKHName())) {
                connectionKH(context,deviceBean.getAddress(),listener);
            } else {
                connectionWXL(context,deviceBean.getAddress(),listener);
            }

        } else {
            BluetoothUtil.newInstance().startConnectWeightBlue((Activity) context, deviceBean1 -> {
                DeviceManger.getDeviceManger().saveWeightDevice(deviceBean1);
                connection(deviceBean1,context,listener);
            });
        }
    }
    public void connection(DeviceBean deviceBean,Context context, OnConnectionBluListener listener){
        if (deviceBean==null || deviceBean.getName()==null)return;
        if (deviceBean.getName().startsWith(WeightBlueType.getKHName())) {
            connectionKH(context,deviceBean.getAddress(),listener);
        } else {
            connectionWXL(context,deviceBean.getAddress(),listener);
        }
    }

    public void destroy(){
        if (mWXLWeightBluetooth!=null){
            mWXLWeightBluetooth.destory();
        }
        if (mKHWeightBluetooth!=null){
            mKHWeightBluetooth.destory();
        }
        mWXLWeightBluetooth=null;
        mKHWeightBluetooth=null;
        weightBlueManger=null;

    }


}
